package es

import (
	"context"
	"course_srv/models/mysql"
	"encoding/json"
	"github.com/elastic/go-elasticsearch/v7/esapi"
	"github.com/qiniu/go-sdk/v7/sms/bytes"
	"log"
	"strconv"
)

func SyncCourseEs(course mysql.Course) error {

	data, err := json.Marshal(course)
	if err != nil {
		log.Fatalf("Error marshaling document: %s", err)
	}

	// Set up the request object.
	req := esapi.IndexRequest{
		Index:      "course",
		DocumentID: strconv.Itoa(int(course.ID)),
		Body:       bytes.NewReader(data),
		Refresh:    "true",
	}

	// Perform the request with the client.
	res, err := req.Do(context.Background(), EsClient)
	if err != nil {
		log.Fatalf("Error getting response: %s", err)
	}
	defer res.Body.Close()
	return nil
}
